/*******************************************************************************
 * AdminServiceDelegator.java
 *
 * Created by Nishan
 * Last modified Date: Sep 9, 2008 2:22:24 PM by nishan
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/

package com.printeverywhere.delegator;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.printeverywhere.dto.Admin;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class AdminServiceDelegator.
 */
public class AdminServiceDelegator {

	/**
	 * Gets the list of Admin Users where username = username .
	 * 
	 * @param username the username
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static Admin getAdminByName(String username) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction tx =session.beginTransaction();
			Criteria criteria = session.createCriteria(Admin.class);
			criteria.add(Restrictions.eq(AttributeConstants.USRNAME ,username));
			List<Admin> adminList = criteria.list();
			tx.commit();
			return (adminList.size() > 0) ? adminList.get(0) : null;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}
	
	public static List<Admin> adminUserList()throws HibernateException {
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(Admin.class);
		Transaction transaction = session.beginTransaction();
		List<Admin> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			throw e;
		} finally {
			HibernateUtil.closeSession();
		}
		return list;
	}
	
	public static void main(String[] args) {
		adminUserList();
	}
}